

#ifndef GUI_COLOR_H__
#define GUI_COLOR_H__

#include <stdint.h> 
#include "xgui_config.h"

// #define COLOR_RGB(R,G,B)    ((R & 0xF8) | ((G >> 5) | ( G << 13) ) | ((B & 0x1F) << 8))
#define RGB(R,G,B)      ((((R >> 3) << 11) | ((G>>2) << 5) | ((B>>3) << 0)) & 0xFFFF)
#define COLOR_RGB(R,G,B)     (((RGB(R,G,B) << 8) | (RGB(R,G,B) >> 8 )) & 0xFFFF)

#define COLOR_WHITE     (0xFFFF)
#define COLOR_BLACK     (0x0000)
#define COLOR_RED       (0x00F8)
#define COLOR_GREEN     (0xE007)
#define COLOR_BLUE      (0x1F00)

#define COLOR_YELLOW    (0xE0FF)    // red + green
#define COLOR_PURPLE    (0x1FF8)    // red + blue
#define COLOR_CYAN      (0xFF07)    // green + blue

#define COLOR_YELLOW1    (0xC0CD)

#define COLOR_PINK      (0x19B8)
#define COLOR_CYAN1     (0x7706)
#define COLOR_ORANGE    (0x00FD)

#define COLOR_MESSAGE_IGNORE    (0xD904)

#define COLOR_GRAY      (0xE738) //
#define COLOR_GRAY2     (0x8631)
#define COLOR_TEST_1    (0x8E36)
#define REAL_COLOR_GRAY (0x0842)


typedef union{
    struct{
        uint16_t b: 5;
        uint16_t g: 6;
        uint16_t r: 5; 
    }rgb565;
    uint16_t value; 
}color_t;

typedef struct
{
    uint8_t status;
    color_t color;
}color_random_t; 

// uint16_t color_mix(uint16_t c1, uint16_t c2, uint8_t ratio);

GUI_RAM_CODE
static
__GUI_INLINE uint16_t color_mix(uint16_t c1, uint16_t c2, uint8_t ratio)
{
    
    color_t color1, color2, colormix ;

    color1.value = (c1 << 8 | c1 >> 8);
    color2.value = (c2 << 8 | c2 >> 8);

    colormix.rgb565.r = ((uint16_t)color1.rgb565.r * ratio + (uint16_t)color2.rgb565.r * (255 - ratio)) >> 8;
    colormix.rgb565.g = ((uint16_t)color1.rgb565.g * ratio + (uint16_t)color2.rgb565.g * (255 - ratio)) >> 8;
    colormix.rgb565.b = ((uint16_t)color1.rgb565.b * ratio + (uint16_t)color2.rgb565.b * (255 - ratio)) >> 8;

    colormix.value = (colormix.value << 8 | colormix.value >> 8 ); 
    
    return colormix.value;
}

uint16_t gui_color_next_color(color_random_t *r);

#endif 
